package GUITOOl;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JScrollBar;
import javax.swing.plaf.basic.BasicScrollBarUI;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;

/**
 * The type MyScrollBar ui.
 *
 * @author wyc, zry
 * @version 1.0
 */
public class MyScrollBarUI extends BasicScrollBarUI {

    /**
     * To get Preferred Size
     * @param c component
     * @return the size
     */
    @Override
    public Dimension getPreferredSize(JComponent c) {
        Dimension preferredSize = super.getPreferredSize(c);
        return new Dimension(10, preferredSize.height);
    }

    /**
     * To paint image in a thumb
     * @param g image
     * @param c component
     * @param thumbBounds thumbBounds
     */
    @Override
    protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
        Graphics2D g2 = (Graphics2D) g;
        // 把绘制区的x，y点坐标定义为坐标系的原点
        g2.translate(thumbBounds.x, thumbBounds.y);
        g2.setColor(Color.GRAY);
        g2.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
        g2.fillRoundRect(0, 0, thumbBounds.width, thumbBounds.height - 1, 0, 0);
    }

    /**
     * Create DecreaseButton
     * @param orientation orientation
     * @return button
     */
    @Override
    protected JButton createDecreaseButton(int orientation) {
        return nullButton();
    }

    /**
     * Create IncreaseButton
     * @param orientation orientation
     * @return button
     */
    @Override
    protected JButton createIncreaseButton(int orientation) {
        return nullButton();
    }

    /**
     * To paint DecreaseHighlight
     * @param g image
     */
    @Override
    protected void paintDecreaseHighlight(Graphics g) {
        g.setColor(trackColor);
        int x = getTrackBounds().x;
        int y = getTrackBounds().y;
        int w = 0, h = 0;
        if (scrollbar.getOrientation() == JScrollBar.VERTICAL) {
            w = getThumbBounds().width;
            h = getThumbBounds().y - y;
        }
        if (scrollbar.getOrientation() == JScrollBar.HORIZONTAL) {
            w = getThumbBounds().x - x;
            h = getThumbBounds().height;
        }
        g.fillRect(x, y, w, h);
    }

    /**
     * To paint IncreaseHighlight
     * @param g image
     */
    @Override
    protected void paintIncreaseHighlight(Graphics g) {
        g.setColor(trackColor);
        int x = getThumbBounds().x;
        int y = getThumbBounds().y;
        int w = getTrackBounds().width;
        int h = getTrackBounds().height;
        g.fillRect(x, y, w, h);
    }

    /**
     * To get a button with no border
     * @return button
     */
    private JButton nullButton() {
        JButton button = new JButton();
        button.setBorder(null);
        return button;
    }

}